package com.ly.heart.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ly.common.utils.DateUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.FileEntity;
import com.ly.heart.service.IFileInfoService;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.ProjectMilestoneMapper;
import com.ly.heart.domain.ProjectMilestoneEntity;
import com.ly.heart.service.IProjectMilestoneService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

import static com.ly.common.utils.SecurityUtils.getLoginUser;

/**
 * 项目里程碑，计划实施节点Service业务层处理
 *
 * @author cxl
 * @date 2024-05-14
 */
@Service
public class ProjectMilestoneServiceImpl implements IProjectMilestoneService {

    @Resource
    private ProjectMilestoneMapper projectMilestoneMapper;
    @Resource
    private IFileInfoService iFileInfoService;

    /**
     * 查询项目里程碑，计划实施节点
     *
     * @param id 项目里程碑，计划实施节点主键
     * @return 项目里程碑，计划实施节点
     */
    @Override
    public ProjectMilestoneEntity selectProjectMilestoneEntityById ( String id ) {
        return projectMilestoneMapper.selectProjectMilestoneEntityById ( id );
    }

    /**
     * 查询项目里程碑，计划实施节点列表
     *
     * @param projectMilestoneEntity 项目里程碑，计划实施节点
     * @return 项目里程碑，计划实施节点
     */
    @Override
    public List<ProjectMilestoneEntity> selectProjectMilestoneEntityList ( ProjectMilestoneEntity projectMilestoneEntity ) {
        return projectMilestoneMapper.selectProjectMilestoneEntityList ( projectMilestoneEntity );
    }

    /**
     * 新增项目里程碑，计划实施节点
     *
     * @param projectMilestoneEntity 项目里程碑，计划实施节点
     * @return 结果
     */
    @Override
    public int insertProjectMilestoneEntity ( ProjectMilestoneEntity projectMilestoneEntity ) {
        projectMilestoneEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        projectMilestoneEntity.setCreateTime ( DateUtils.getNowDate () );
        projectMilestoneEntity.setUserId ( String.valueOf(getLoginUser().getUser().getUserId()) );
        //处理附件列表
        if ( ! CollectionUtils.isEmpty ( projectMilestoneEntity.getFileIds () ) ) {
            List<FileEntity> fileEntities = new ArrayList<> ();
            projectMilestoneEntity.getFileIds ().forEach ( fileId -> {
                FileEntity fileEntity = new FileEntity ();
                fileEntity.setRelationId ( projectMilestoneEntity.getId () );
                fileEntity.setId ( fileId );
                fileEntity.setChangeTime ( DateUtils.getNowDate () );
            } );
            boolean b = iFileInfoService.updateBatchById ( fileEntities );
            if ( ! b ) {
                throw new RuntimeException ( "修改项目计划流程附件失败" );
            }
        }
        return projectMilestoneMapper.insertProjectMilestoneEntity ( projectMilestoneEntity );
    }

    /**
     * 修改项目里程碑，计划实施节点
     *
     * @param projectMilestoneEntity 项目里程碑，计划实施节点
     * @return 结果
     */
    @Override
    public int updateProjectMilestoneEntity ( ProjectMilestoneEntity projectMilestoneEntity ) {
        return projectMilestoneMapper.updateProjectMilestoneEntity ( projectMilestoneEntity );
    }

    /**
     * 批量删除项目里程碑，计划实施节点
     *
     * @param ids 需要删除的项目里程碑，计划实施节点主键
     * @return 结果
     */
    @Override
    public int deleteProjectMilestoneEntityByIds ( String[] ids ) {
        return projectMilestoneMapper.deleteProjectMilestoneEntityByIds ( ids );
    }

    /**
     * 删除项目里程碑，计划实施节点信息
     *
     * @param id 项目里程碑，计划实施节点主键
     * @return 结果
     */
    @Override
    public int deleteProjectMilestoneEntityById ( String id ) {
        return projectMilestoneMapper.deleteProjectMilestoneEntityById ( id );
    }
}
